/************************************************************************
 ** This file is part of the network simulator Shawn.                  **
 ** Copyright (C) 2004-2007 by the SwarmNet (www.swarmnet.de) project  **
 ** Shawn is free software; you can redistribute it and/or modify it   **
 ** under the terms of the BSD License. Refer to the shawn-licence.txt **
 ** file in the root of the Shawn source tree for further details.     **
 ************************************************************************/
#include "legacyapps/simple_app/simple_app_processor.h"
#ifdef ENABLE_SIMPLE_APP

#include "legacyapps/simple_app/simple_app_message.h"
#include "sys/simulation/simulation_controller.h"
#include "sys/node.h"
#include <iostream>


namespace simple_app
{

   SimpleAppProcessor::
   SimpleAppProcessor()
   {}

   // ----------------------------------------------------------------------
   SimpleAppProcessor::
   ~SimpleAppProcessor()
   {}
   // ----------------------------------------------------------------------
   void
   SimpleAppProcessor::
   boot( void )
      throw()
      {
	   // write actual number of known neighbors to tag
	   write_int_tag( owner_w(), "neighbors", 0 );
	   write_double_tag(owner_w(), "Battery",0.5);

      }

   // ----------------------------------------------------------------------
   bool SimpleAppProcessor::process_message(const shawn::ConstMessageHandle& mh) throw () {
	const SimpleAppMessage* msg =
			dynamic_cast<const SimpleAppMessage*> (mh.get());

	if (msg != NULL)

		if (owner() != msg->source()) {

			neighbours_.insert(&msg->source());
			INFO( logger(), "Received message from '"
					<< msg->source().label()
					<< "'" );

			// write actual number of known neighbors to tag
			write_int_tag(owner_w(), "neighbors", neighbours_.size());
			write_double_tag(owner_w(), "Battery",0.5);


			return true;

		}
	return Processor::process_message(mh);
}
   // ----------------------------------------------------------------------
   void
   SimpleAppProcessor::
   work( void )
      throw()
      {

      // send message only in the first simulation round
      if ( simulation_round() == 0 )

         send( new SimpleAppMessage );


}
   // ----------------------------------------------------------------------
   void
   SimpleAppProcessor::
   write_int_tag( shawn::Node& node, const std::string& tag_name, int value )
      throw()
      {

      shawn::TagHandle tag = node.find_tag_w( tag_name );

      if ( tag.is_not_null() ){

         shawn::IntegerTag* intt = dynamic_cast<shawn::IntegerTag*>( tag.get() );


         if ( intt != NULL ){

            intt->set_value( value );
         }
      }
      else{

         shawn::IntegerTag *intt = new shawn::IntegerTag( tag_name, value );
         node.add_tag( intt );
      }


}
   // ----------------------------------------------------------------------
   void
   SimpleAppProcessor::
   write_double_tag(shawn::Node& node, const std::string& tag_name, double value) throw ()

   {
	      shawn::TagHandle tag = node.find_tag_w( tag_name );

	      if ( tag.is_not_null() ){

	    	  shawn::DoubleTag* dblt = dynamic_cast<shawn::DoubleTag*>( tag.get() );

	    	  if ( dblt != NULL ){

	    	  	            dblt->set_value( value );
	    	  	         }
	    	  	      }
	      else{

	    	  shawn::DoubleTag* dblt = new shawn::DoubleTag( tag_name, value );
	      	         node.add_tag( dblt );
	      	      }


	      }

/*
	      if ( tag.is_not_null() ){

	         shawn::IntegerTag* intt = dynamic_cast<shawn::IntegerTag*>( tag.get() );


	         if ( intt != NULL ){

	            intt->set_value( value );
	         }
	      }
	      else{

	         shawn::IntegerTag *intt = new shawn::IntegerTag( tag_name, value );
	         node.add_tag( intt );
	      }
*/







   // ----------------------------------------------------------------------
}
#endif
